package edu.njfu.mystudentapp.adapter


import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import edu.njfu.mystudentapp.R
import edu.njfu.mystudentapp.StudentInformationActivity
import edu.njfu.mystudentapp.model.Student
import edu.njfu.mystudentapp.service.StudentService
import edu.njfu.mystudentapp.utils.ServerCreator

import okhttp3.ResponseBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Func1
import rx.schedulers.Schedulers



class StudentAdapterByRecyclerView(val stduents: ArrayList<Student>):
        RecyclerView.Adapter<StudentAdapterByRecyclerView.ViewHolder>(){
    var bitmap:Bitmap?=null
    inner class ViewHolder(view: View):RecyclerView.ViewHolder(view){
        var studenName:TextView=view.findViewById(R.id.studenName)
        var studenNunber:TextView=view.findViewById(R.id.studentNumber)
        var studentImage:ImageView=view.findViewById(R.id.stuImage)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
//        TODO("Not yet implemented")
        val view=LayoutInflater.from(parent.context)
            .inflate(R.layout.student_info_layout,parent,false)
        var viewHolder=ViewHolder(view)

        //事项方法一
        viewHolder.itemView.setOnClickListener{
            val position = viewHolder.layoutPosition //得到点击位置

            val stduent  = stduents.get(position) //由位置得到实例

//            Toast.makeText(view.context,stduent.toString(),Toast.LENGTH_LONG).show()
            var intent12= Intent(view.context, StudentInformationActivity::class.java)
            intent12.putExtra("studentInformation",Gson().toJson(stduent))
            view.context.startActivity(intent12)
        }
        viewHolder.studentImage.setOnClickListener{
            val position=viewHolder.layoutPosition
            viewHolder.studentImage.maxHeight=100
        }


        return viewHolder
    }


    override fun getItemCount(): Int{
        return stduents.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//        TODO("Not yet implemented")
        val student=stduents[position]
        holder.studenName.text=student.stuName
        holder.studenNunber.text=student.stuNo
        if (student.gender=="女"){
            loadImage(holder.studentImage,1)
        }else{
            loadImage(holder.studentImage,0)
        }
    }

    private fun loadImage(imageView: ImageView, sex:Int){
       var url:String
        if(sex==1){
            url="girl.jpg"
        }else{
            url="boy.jpeg"
        }
        ServerCreator.create(StudentService::class.java)
            .downloadPic(url)
            .map(object: Func1<ResponseBody, Bitmap> {
                override fun call(t: ResponseBody?):
                        Bitmap {
                    return BitmapFactory.decodeStream(t?.byteStream())
                }
            }).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object: Observer<Bitmap> {
                override fun onCompleted() {
                    Log.d("finished","finished.................")
                }
                override fun onError(e: Throwable?) {
                    Log.d("error","error................")
                }
                override fun onNext(t: Bitmap?) {
                    imageView.setImageBitmap(t)
                }
            })
    }
}